package com.cosina.test;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLUtils;
import android.os.SystemClock;
import android.util.Log;

public class Lession18Render extends BaseRender{
	boolean light = true;
	
	int part1, part2;
	int object;
	
	float xrot, yrot;
	float xspeed, yspeed;
	float z = -5.0f;
	;
	FloatBuffer lightAmbient = FloatBuffer.wrap(new float[]{0.5f,0.5f,0.5f,1.0f}); 
	FloatBuffer lightDiffuse = FloatBuffer.wrap(new float[]{1.0f,1.0f,1.0f,1.0f}); 
	FloatBuffer lightPosition = FloatBuffer.wrap(new float[]{0.0f,0.0f,2.0f,1.0f}); 
	
	int filter = 1;
	
	int [] texture;
	
	GLU glu = new GLU();
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		super.onSurfaceCreated(gl, config);
		
		IntBuffer textureBuffer = IntBuffer.allocate(3);
		gl.glGenTextures(3, textureBuffer);
		texture = textureBuffer.array();
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[0]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_NEAREST); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, DataManager.getBm(R.drawable.crate), 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[1]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, DataManager.getBm(R.drawable.crate), 0);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[2]);
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR); // ( NEW )
		gl.glTexParameterx(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR); // ( NEW )
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, DataManager.getBm(R.drawable.crate), 0);
		
		gl.glClearDepthf(1.0f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		/* Setup The Ambient Light */
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, lightAmbient);

	    /* Setup The Diffuse Light */
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, lightDiffuse);

	    /* Position The Light */
	    gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, lightPosition);

	    gl.glEnable(GL10.GL_LIGHT1);
	    
	}
	int one = 0x10000;
	@Override
	public void onDrawFrame(GL10 gl) {
		super.baseDrawFrame(gl);
		gl.glEnable(GL10.GL_LIGHTING);
		
		IntBuffer vertices = IntBuffer.wrap(new int[]{
				-one,-one,one,
				one,-one,one,
				one,one,one,
				-one,one,one,
				
				-one,-one,-one,
				-one,one,-one,
				one,one,-one,
				one,-one,-one,
				
				-one,one,-one,
				-one,one,one,
				one,one,one,
				one,one,-one,
				
				-one,-one,-one,
				one,-one,-one,
				one,-one,one,
				-one,-one,one,
				
				one,-one,-one,
				one,one,-one,
				one,one,one,
				one,-one,one,
				
				-one,-one,-one,
				-one,-one,one,
				-one,one,one,
				-one,one,-one,
				
		});
		
		IntBuffer normals = IntBuffer.wrap(new int[]{
				0,0,one,
				0,0,one,
				0,0,one,
				0,0,one,
				
				0,0,one,
				0,0,one,
				0,0,one,
				0,0,one,
				
				0,one,0,
				0,one,0,
				0,one,0,
				0,one,0,
				
				0,-one,0,
				0,-one,0,
				0,-one,0,
				0,-one,0,
				
				one,0,0,
				one,0,0,
				one,0,0,
				one,0,0,
				
				-one,0,0,
				-one,0,0,
				-one,0,0,
				-one,0,0,
		});
		
		IntBuffer texCoords = IntBuffer.wrap(new int[]{
			one,0,0,0,0,one,one,one,	
			0,0,0,one,one,one,one,0,
			one,one,one,0,0,0,0,one,
			0,one,one,one,one,0,0,0,
			0,0,0,one,one,one,one,0,
			one,0,0,0,0,one,one,one,
		});
		
		ByteBuffer indices = ByteBuffer.wrap(new byte[]{
				0,1,3,2,
				4,5,7,6,
				8,9,11,10,
				12,13,15,14,
				16,17,19,18,
				20,21,23,22,
		});
		
		gl.glTranslatef(0.0f, 0.0f, z);
		
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[filter]);
		
		gl.glNormalPointer(GL10.GL_FIXED, 0, normals);
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices);
		gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords);

		gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24,  GL10.GL_UNSIGNED_BYTE, indices);
	    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
	    xrot+=0.3f; /* X Axis Rotation */
	    yrot+=0.2f; /* Y Axis Rotation */
	    
	    
	    long curent = SystemClock.currentThreadTimeMillis();
	    if(curent - time > 3000){
	    	light = !light;
	    	time = curent;
	    }
	}
	long time = 0;
}